package com.jumi.microservice.controller.taobao;

import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.common.utils.IpUtil;
import com.jumi.microservice.constant.taobao.MaterialEnum;
import com.jumi.microservice.dto.taobao.*;
import com.jumi.microservice.jwt.annon.IgnoreUrlsAnnon;
import com.jumi.microservice.service.ITaoBaoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Dirk
 * @Date 2021-01-06 14:31
 */
@RestController
@RequestMapping("/api/taobao")
@Api(tags = {"C端-淘宝"})
public class TaoBaoController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(TaoBaoController.class);

    @DubboReference
    private ITaoBaoService taoBaoService;

    @GetMapping("/oauth/h5")
    @ApiOperation("淘宝授权回调地址")
    public ResponseResult<Boolean> oauthCallback(OauthRequest oauthRequest) {
        oauthRequest.setUid(getApiUserId().longValue());
        return success(taoBaoService.oauthH5(oauthRequest));
    }

    @PostMapping("/oauth")
    @ApiOperation("淘宝授权绑定")
    public ResponseResult<Boolean> oauth(OauthRequest oauthRequest) {
        oauthRequest.setUid(getApiUserId().longValue());
        return success(taoBaoService.oauth(oauthRequest));
    }

    @PostMapping("/favorites")
    @IgnoreUrlsAnnon("/favorites")
    @ApiOperation("获取选品库列表")
    public ResponseResult<List<FavoritesResponse>> getFavorites() {
        return success(taoBaoService.getFavorites());
    }

    @PostMapping("/material")
    @IgnoreUrlsAnnon("/material")
    @ApiOperation("获取商品列表(选品库、热销榜、高佣榜、猜你喜欢)")
    public TableDataInfo<MaterialResponse> getMaterial(MaterialRequest materialRequest) {
        if (materialRequest.getMaterialEnum() == null) {
            materialRequest.setMaterialEnum(MaterialEnum.FAVORITES);
        }
        return taoBaoService.getMaterial(materialRequest);
    }

    @PostMapping("/material/optional")
    @IgnoreUrlsAnnon("/material/optional")
    @ApiOperation("物料搜索")
    public TableDataInfo<MaterialResponse> searchMaterial(SearchRequest searchRequest) {
        searchRequest.setIp(IpUtil.getClientIp(request));
        return taoBaoService.searchMaterial(searchRequest);
    }

    @PostMapping("/item/info")
    @IgnoreUrlsAnnon("/item/info")
    @ApiOperation("商品详情")
    public ResponseResult<ItemInfoResponse> getItemInfo(ItemInfoRequest itemInfoRequest, HttpServletRequest httpServletRequest) {
        itemInfoRequest.setIp(IpUtil.getClientIp(httpServletRequest));
        return success(taoBaoService.getItemInfo(itemInfoRequest));
    }

    @PostMapping("/oauth/status")
    @ApiOperation("淘宝绑定状态")
    public ResponseResult<Boolean> getOauthStatus() {
        Long userTbSpecialId = taoBaoService.getUserTbSpecialId(getApiUserId().longValue());
        return success(!userTbSpecialId.equals(0L));
    }

    @PostMapping("/pwd/create")
    @IgnoreUrlsAnnon("/pwd/create")
    @ApiOperation("创建淘口令")
    public ResponseResult<PwdResponse> createPwd(PwdRequest pwdRequest) {
        return success(taoBaoService.createPwd(pwdRequest));
    }
}
